﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class WolneController : Controller
    {
        //
        // GET: /Wplne/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {
                return View(context.Wolne.Select(n => new WolneViewModel
                {
                    ID = n.ID_Wolne,
                    IDPowodu =(int) n.ID_Powodu,
                    IDPracownika = n.ID_Wolne,
                    DataM = (DateTime)n.DataM,
                    DataU = (DateTime)n.DataU

                }).ToArray()
                );
            }
        }

        //
        // GET: /Wplne/Details/5

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                Wolne wolneDB = context.Wolne.SingleOrDefault(n => n.ID_Wolne == id);
                if (wolneDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new WolneViewModel(wolneDB));
            }
        }

        //
        // GET: /Wplne/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Wplne/Create

        [HttpPost]
        public ActionResult Create(WolneViewModel wolne)
        {
            try
            {
                using (var context = new HREntities())
                {
                    context.Wolne.AddObject(wolne.getModel(new Wolne()));
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult DeleteGroup(IEnumerable<WolneViewModel> wolne)
        {
            try
            {
                using (var context = new HREntities())
                {
                    //foreach (var item in collection)
                    //{
                    //    context.Wolne.AddObject(wolne.getModel(new Wolne()));
                    //}
                   
                    //context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        //
        // GET: /Wplne/Edit/5
 
        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                Wolne wolneDB = context.Wolne.SingleOrDefault(n => n.ID_Wolne == id);
                if (wolneDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new WolneViewModel(wolneDB));
            }
        }

        //
        // POST: /Wplne/Edit/5

        [HttpPost]
        public ActionResult Edit(WolneViewModel wolne)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Wolne wolneDB = context.Wolne.SingleOrDefault(n => n.ID_Wolne == wolne.ID);
                    context.ApplyCurrentValues<Wolne>("Wolne", wolne.getModel(wolneDB));
                    context.SaveChanges();
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Wplne/Delete/5
 
        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                Wolne wolneDB = context.Wolne.SingleOrDefault(n => n.ID_Wolne == id);
                if (wolneDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new WolneViewModel(wolneDB));
            }
        }

        //
        // POST: /Wplne/Delete/5

        [HttpPost]
        public ActionResult Delete(WolneViewModel wolne)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Wolne wolneDB = context.Wolne.SingleOrDefault(n => n.ID_Wolne == wolne.ID);
                    if (wolneDB != null)
                    {
                        context.Wolne.DeleteObject(wolneDB);
                        context.SaveChanges();
                    }
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
